import React, { useEffect, useState } from 'react'
import ProForm, { ProFormText,ProFormSelect } from '@ant-design/pro-form';
import { Modal, Skeleton } from 'antd';
import { showOrders, orderDelivery } from '@/services/orders'
import { message } from 'antd';

export default function CreateOrEdit(props) {
  //表单数据
  const [initialValues, setInitialValues] = useState(undefined)
  //模态框是否显示
  //操作模态框显示隐藏的方法
  //父组件的传递过来的表格的引用，可以用来操作表格，比如刷新表格
  //要编辑的ID，添加的时候用undefined，只有编辑才有值
  const { isModalVisible, isShowModal, actionRef, editId } = props
  //提交表单，执行编辑或添加
  const handleSubmit = async values => {
    let res = {}
    if (editId) {
      res = await orderDelivery(editId, values)
    }
    console.log(res);
    if (res.status === undefined) {
      message.success(`上传成功`)
      //刷新表格数据
      actionRef.current.reload()
      //关闭模态框
      isShowModal(false)
    }
  }

  useEffect(() => {
    const getOneUser = async () => {
      if (editId !== undefined) {
        const res = await showOrders(editId)
        console.log(res);
        //获取数据之后，修改状态，状态改变，组件重新渲染，骨架屏消失，表单出现
        setInitialValues({
          express_type:res.express_type,
          express_no:res.express_no
        })
      }
    }
    getOneUser()
  }, [])
  return (
    <Modal title={`订单发货`} visible={isModalVisible}
      onCancel={() => isShowModal(false)} footer={null} destroyOnClose={true}>
      {
        //只有在编辑的情况下，并且要显示的数据还没有返回，才显示骨架屏
        initialValues === undefined && editId !== undefined ? <Skeleton active={true} paragraph={{ rows: 2 }} /> :
          <ProForm
            initialValues={initialValues}
            onFinish={values => handleSubmit(values)}
          >
            <ProFormSelect
              name="express_type"
              label="快递类型"
              placeholder="请选择快递类型"
              valueEnum={{
                SF: '顺丰',
                YTO: '圆通',
                YD: '韵达'
              }}
              rules={[
                { required: true, message: '请选择快递类型！' }
              ]}
            />
            <ProFormText
              name="express_no"
              label="快递单号"
              placeholder="请输入快递单号！"
              rules={[
                { required: true, message: '请输入快递单号！' },
              ]}
            />


          </ProForm>
      }

    </Modal>
  )
}
